package com.spica.spicaschool.ui.circle

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.navigation.Navigation
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.CircleCropTransformation
import com.drakeet.multitype.ItemViewDelegate
import com.kongzue.dialogx.dialogs.TipDialog
import com.spica.spicaschool.R
import com.spica.spicaschool.bean.diary.Diary
import com.spica.spicaschool.databinding.ItemDiaryBinding
import com.spica.spicaschool.network.Url
import com.spica.spicaschool.utils.SpUtils
import com.spica.spicaschool.utils.TOKEN_SIGN
import com.spica.spicaschool.utils.toUid
import org.ocpsoft.prettytime.PrettyTime
import rxhttp.wrapper.param.RxHttp
import java.text.SimpleDateFormat
import java.util.*

/**
 * 动态列表的适配器
 */
class DiaryItemDelegate constructor(private val activity: Activity) :
  ItemViewDelegate<Diary, DiaryItemDelegate.ViewHolder>() {


  private val prettyTime = PrettyTime(Locale.CHINA)

  private val navController =
    Navigation.findNavController(activity, R.id.fragment_container_view)

  override fun getItemId(item: Diary): Long = item.diaryUid.toUid().toLong()

  @SuppressLint("SetTextI18n")
  override fun onBindViewHolder(holder: ViewHolder, item: Diary) {
    val isLike = item.isLike


    val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
    val date = sdf.parse(item.diaryTime)

    // UI填充
    with(holder.viewBinding) {
      ivAvatar.load(item.user.uAvatar) {
        placeholder(R.drawable.default_head)
        error(R.drawable.default_head)
        transformations(CircleCropTransformation())
      }
      tvAuthor.text = item.user.uNickname
      tvPushType.text = "${item.user.uRank}级 发布动态"
      tvInfo.text = item.diaryContentPreview
      tvPushTime.text = prettyTime.format(date)
      tvTitle.text = item.diaryTitle
      tvLikeNum.text = "${item.diaryLikeNum}"
      tvReplyNum.text = "${item.diaryCommentNum}"
      // 点击查看详情
      root.setOnClickListener {
        val bundle = bundleOf(Pair("DiaryId", item.diaryId))
        navController.navigate(R.id.action_home_to_diary_see, bundle)
      }
      btnLike.setOnClickListener {
        when (isLike) {
          0 -> {
            // likeAnim.show()
            holder.viewBinding.btnLike.load(R.drawable.ic_like_selected)
            RxHttp.postForm(Url.LIKE_ADD)
              .add("token", SpUtils.getSpValue(context = activity, key = TOKEN_SIGN, defaultVal = ""))
              .add("id", item.diaryId)
              .add("type", 1)
              .asOkResponse()
              .subscribe(
                {
                  if (it.code == 200) {
                    btnLike.load(R.drawable.ic_like_selected)
                    btnLike.setOnClickListener(null)//清除点击事件
                  } else {
                    TipDialog.show(it.message)
                  }
                }, {
                  TipDialog.show(it.message)
                }
              )
          }
          else -> {
            // unlikeAnim.show()
            holder.viewBinding.btnLike.load(R.drawable.ic_like_normal)
          }
        }
      }
    }
  }

  override fun onCreateViewHolder(
    context: Context,
    parent: ViewGroup
  ): ViewHolder {
    val viewBinding = ItemDiaryBinding.inflate(LayoutInflater.from(context), parent, false)
    return ViewHolder(viewBinding)
  }

  // 视图绑定
  class ViewHolder(val viewBinding: ItemDiaryBinding) : RecyclerView.ViewHolder(viewBinding.root)
}
